const { defineConfig } = require('@vue/cli-service');
const path = require('path');
const CompressionWebpackPlugin = require('compression-webpack-plugin');

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = defineConfig({
  publicPath: process.env.VUE_APP_BASE_URL,
  chainWebpack: (config) => {
    config.resolve.alias.set('@', resolve('src'));
    config.module.rules.delete('svg');

    if (['production'].includes(process.env.NODE_ENV)) {
      // 配置压缩插件
      config.plugin('CompressionWebpackPlugin').use(CompressionWebpackPlugin, [
        {
          algorithm: 'gzip',
          test: /\.(js|css|html|svg)$/,
          threshold: 10240, // 文件大小大于等于 10 KB 才会被压缩
          minRatio: 0.8, // 压缩比例大于等于 0.8 才会被压缩
        },
      ]);
    }
  },
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.svg$/,
          loader: 'vue-svg-loader',
        },
      ],
    },
    optimization: {
      splitChunks: {
        chunks: 'all',
        cacheGroups: {
          lodash: {
            test: /[\\/]node_modules[\\/]lodash-es[\\/]/,
            name: 'vendor/lodash',
            chunks: 'all',
            minChunks: 1,
          },
          tdesign: {
            test: /[\\/]node_modules[\\/]tdesign-vue[\\/]/,
            name: 'chunk-tdesign',
            priority: 10,
            chunks: 'all',
          },
          tdesignIcons: {
            test: /[\\/]node_modules[\\/]tdesign-icons-vue[\\/]/,
            name: 'chunk-tdesign-icons',
            chunks: 'all',
            minChunks: 1,
          },
          dayjs: {
            test: /[\\/]node_modules[\\/]dayjs[\\/]/,
            name: 'vendor/dayjs',
            chunks: 'all',
            minChunks: 1,
          },
          echarts: {
            test: /[\\/]node_modules[\\/]echarts[\\/]/,
            name: 'vendor/echarts',
            chunks: 'all',
            minChunks: 1,
          },
          vendors: {
            test: /[\\/]node_modules[\\/]/,
            name: 'chunk-vendors',
            priority: -10,
            chunks: 'initial',
          },
          common: {
            name: 'chunk-common',
            minChunks: 2,
            priority: -20,
            chunks: 'initial',
            reuseExistingChunk: true,
          },
        },
      },
    },
  },
  productionSourceMap: false,
  lintOnSave: false,
  transpileDependencies: [],
  devServer: {
    headers: {
      'Access-Control-Allow-Origin': '*',
    },
    port: '8086',
    historyApiFallback: true,
    proxy: {
			 '/dev_survey/*': {
        target: 'http://127.0.0.1:3000',
        changeOrigin: true,
        pathRewrite: {
          '^/dev_survey': '/api',
        },
      },
			'/api/*': {
        target: 'http://127.0.0.1:7001',
        changeOrigin: true,
      },
      '/management/*': {
        target: 'http://127.0.0.1:8080', // 子项目服务器地址
        changeOrigin: true,
      }
    },
  },
});
